<template>
  <div class="mod-log">

    <avue-crud ref="crud" :page="page" :data="dataList" :option="tableOption" @search-change="searchChange"
      @selection-change="selectionChange" @on-load="getDataList">
      <template slot="menuLeft">
        <el-button v-if="isAuth('shop:notice:save')" type="primary" size="small" icon="el-icon-remove"
          @click="deleteHandle()" :disabled="dataListSelections.length <= 0">删除</el-button>
        <el-button v-if="isAuth('shop:notice:save')" type="primary" size="small" icon="el-icon-delete"
          @click="clearHandle()">清空</el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { tableOption } from '@/crud/sys/log'
export default {
  data() {
    return {
      dataList: [],
      dataListLoading: false,
      selectionDataList: [],
      dataListSelections: [],
      tableOption: tableOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      }
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList(page, params, done) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/log/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            params
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
        if (done) {
          done()
        }
      })
    },
    // 条件查询
    searchChange(params, done) {
      this.getDataList(this.page, params, done)
    },
    selectionChange(val) {
      this.dataListSelections = val
    },
    deleteHandle(id) {
      var userIds = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/log'),
          method: 'delete',
          data: this.$http.adornData(userIds, false)
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      }).catch(() => { })
    },
    clearHandle() {
      this.$confirm(`确定清空日志操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/log/clear'),
          method: 'post',
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      }).catch(() => { })
    }

  }
}
</script>
